var code = getQueryString("code");
$(function () {
    getUserInfo();
    var channel = getQueryString("channel");
    $(".back-wrapper").click(function () {
        location.href = "./mine.html?channel="+channel;
    });
    $(".home-wrapper").click(function () {
        location.href = "../index.html?channel="+channel;
    });
    $(".recharge-way-wrapper .way").click(function () {
        $(this).addClass("active").siblings(".way").removeClass("active");
    });
    getRechargeConfig();
    $(".recharge-type-wrapper").on("click","li",function () {
        if($(this).attr("data-enable") == "false"){
            return
        }else{
            $(this).addClass("active").siblings("li").removeClass("active");
            $(".recharge-num").html($(this).children(".pay-price").html());
            code = $(this).attr("data-code");
        }
    });
    $(".check-box").click(function () {
        $(this).toggleClass("selected");
    });
    $(".explain-btn").click(function () {
        $.ajax({
            type:'get',
            url:BASE_URl + "config/index?channel=" + channel,
            success:function (data) {
                if(data.success){
                    $(".explain-item-desc .qq span").html(data.data.app.qq);
                    $(".explain-mask").show();
                }
            }
        });
    });
    $(".close-icon").click(function () {
        $(".explain-mask").hide();
    });
    $(".recharge-btn").click(function () {
        if(!$(".check-box").hasClass("selected")){
            Toast("请先勾选充值条款说明");
        }else{
            if(!code){
                Toast("请选择充值金额");
            }else{
                if($(".way.active").attr("data-way") == "wechat"){
                    //alert("调起微信支付");
                    $.ajax({
                        type:'get',
                        url:BASE_URl + "pay/apply/wechat?code=" + code,
                        success:function (data) {
                            if(data.success){
                                if (typeof WeixinJSBridge == "undefined") {
                                    if (document.addEventListener) {
                                        document.addEventListener("WeixinJSBridgeReady",onBridgeReady(), false);
                                    } else if (document.attachEvent) {
                                        document.attachEvent("WeixinJSBridgeReady", onBridgeReady());
                                        document.attachEvent("onWeixinJSBridgeReady",onBridgeReady());
                                    }
                                }else {
                                    onBridgeReady(data.data);
                                }
                            }else{
                                Toast(data.data.ercode);
                            }
                        }
                    })
                }else{
                    //alert("调起支付宝支付");
                    location.href = "/view/pay/apply/alipay?code="+code;
                }
            }
        }
    });
});

function getRechargeConfig() {
    $.ajax({
        type:'get',
        url:BASE_URl + "config/pay",
        success:function (data) {
            if(data.success){
                DrawRechargeConfig(data.data);
            }
        }
    });
}

function DrawRechargeConfig(data) {
    $(".first-recharge-tip").html(data.tip);
    var html = "";
    if(data.item.length%2 == 1){
        html += `<li class="border-1px-all inline odd-first pr" data-code="${data.item[0].code}" data-enable="${data.item[0].enable}">
                    <div class="pay-name tc"><span>${data.item[0].name}</span>书币</div>
                    <div class="pay-price tc">${data.item[0].price}</div>
                    <div class="pay-desc tc">${data.item[0].desc}</div>
                    <i class="logo-icon pa ${data.item[0].logo}"></i>
                </li>`;
        for(var i = 1;i < data.item.length;i ++){
            if(data.item[i].code == "month" || data.item[i].code == "year"){
                html += `<li class="border-1px-all inline odd pr" data-code="${data.item[i].code}" data-enable="${data.item[0].enable}">
                            <div class="pay-name tc"><span class="month-and-year">${data.item[i].name}</span></div>
                            <div class="pay-price tc">${data.item[i].price}</div>
                            <div class="pay-desc tc">${data.item[i].desc}</div>
                            <i class="logo-icon pa ${data.item[i].logo}"></i>
                         </li>`;
            }else{
                html += `<li class="border-1px-all inline odd pr" data-code="${data.item[i].code}" data-enable="${data.item[0].enable}">
                            <div class="pay-name tc"><span>${data.item[i].name}</span>书币</div>
                            <div class="pay-price tc">${data.item[i].price}</div>
                            <div class="pay-desc tc">${data.item[i].desc}</div>
                            <i class="logo-icon pa ${data.item[i].logo}"></i>
                        </li>`;
            }
        }
    }else{
        for(var i = 0;i < data.item.length;i ++){
            if(data.item[i].code == "month" || data.item[i].code == "year"){
                html += `<li class="border-1px-all inline pr" data-code="${data.item[i].code}" data-enable="${data.item[0].enable}">
                            <div class="pay-name tc"><span class="month-and-year">${data.item[i].name}</span></div>
                            <div class="pay-price tc">${data.item[i].price}</div>
                            <div class="pay-desc tc">${data.item[i].desc}</div>
                            <i class="logo-icon pa ${data.item[i].logo}"></i>
                         </li>`;
            }else{
                html += `<li class="border-1px-all inline pr" data-code="${data.item[i].code}" data-enable="${data.item[0].enable}">
                            <div class="pay-name tc"><span>${data.item[i].name}</span>书币</div>
                            <div class="pay-price tc">${data.item[i].price}</div>
                            <div class="pay-desc tc">${data.item[i].desc}</div>
                            <i class="logo-icon pa ${data.item[i].logo}"></i>
                        </li>`;
            }
        }
    }
    $(".recharge-type-wrapper ul").html(html);

    if(code){
        var el = $(".recharge-type-wrapper ul li[data-code=" + code + "]");
        if(el.attr("data-enable") != "false"){
            el.addClass("active");
            $(".recharge-num").html(el.children(".pay-price").html());
        }
    }
}

function getUserInfo() {
    $.ajax({
        type:'get',
        url:BASE_URl + "user/profile",
        success:function (data) {
            if(data.success){
                $(".rest-coin-wrapper span").html(data.data.coin);
            }
        }
    });
}

// 微信支付
function onBridgeReady(param) {
    WeixinJSBridge.invoke("getBrandWCPayRequest", param, function(res) {
        if (res.err_msg == "get_brand_wcpay_request:ok") {
            // 支付成功
            Toast("支付成功");
            location.reload();
        }
        if (res.err_msg == "get_brand_wcpay_request:cancel") {
            // 取消支付
            Toast("支付已取消");
            $.ajax({
                type:'get',
                url:BASE_URl + "pay/cancel/wechat?orderid=" + param.package.split("=")[1],
                success:function (data) {}
            });
        }
        if (res.err_msg == "get_brand_wcpay_request:fail") {
            // 支付失败
            Toast("支付失败");
        }
    });
}